/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.beans.store;

import ecom.beans.account.*;
import ecom.beans.address.*;

import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class StoreEntityFacade implements StoreEntityFacadeRemote {

    @PersistenceContext
    private EntityManager em;

    public StoreEntity addStore(String storeName,
            int seatsNumber,
            int deliverymenNumber,
            AddressEntity address,
            AccountEntity account) {

        List<StoreEntity> storeList = null;

        storeList = (List<StoreEntity>) em.createNamedQuery("findStoreByName").setParameter("storeName", storeName).getResultList();

        if (storeList.size() == 0 || storeList == null) {

            StoreEntity newStore = new StoreEntity();

            newStore.setStoreName(storeName);
            newStore.setSeatsNumber(seatsNumber);
            newStore.setDeliverymenNumber(deliverymenNumber);
            newStore.setStoreAddress(address);
            newStore.setStoreAccount(account);

            em.persist(newStore);

            return newStore;

        } else {
            return null;
        }
    }

    /*
    public StoreEntity addStore(String name,String address,AccountEntity account){
    
    List<StoreEntity> storeList = null;
    
    storeList = (List<StoreEntity>) em.createNamedQuery("findStoreByName").setParameter("storeName", name).getResultList();
    
    if (storeList.size() == 0 || storeList == null) {
    
    StoreEntity newStore = new StoreEntity();
    
    newStore.setStoreName(name);
    
    newStore.setStoreAddress(address);
    
    account.setAccountType(2);
    
    newStore.setStoreAccount(account);
    
    em.persist(newStore);
    
    return newStore;
    
    } 
    else {
    return null;
    }
    
    }
     */
    public List<StoreEntity> getAllStores() {

        Query getStore;

        try {

            getStore = em.createNamedQuery("findAllStores");


        } catch (Exception e) {

            getStore = null;
        }

        return (List<StoreEntity>) getStore.getResultList();
    }

    public StoreEntity getStoreById(int id) {

        StoreEntity store = null;
        Long longid = new Long(id);

        try {

            store = (StoreEntity) em.createNamedQuery("findStoreById").setParameter("id", longid).getSingleResult();

        } catch (Exception e) {

            store = null;

        }

        return store;
    }

    public List<StoreEntity> removeAllStore() {

        List<StoreEntity> list;

        try {

            list = getAllStores();

            if (list != null && list.size() > 0) {

                for (int i = 0; i < list.size(); i++) {

                    em.remove(list.get(i));
                }
            } else {
                list = null;
            }
        } catch (Exception e) {

            list = null;
        }

        return list;
    // not sure yet

    }

    public StoreEntity getStoreByName(String name) {


        List<StoreEntity> storelist = null;
        StoreEntity store = null;

        try {

            storelist = (List<StoreEntity>) em.createNamedQuery("findStoreByName").setParameter("storeName", name).getResultList();

            if (storelist.size() != 1 || storelist == null) {
                store = null;
            } else {
                store = (StoreEntity) em.createNamedQuery("findStoreByName").setParameter("storeName", name).getSingleResult();
            }
        } catch (Exception e) {

            store = null;

        }

        return store;

    }

    public StoreEntity removeStoreByName(String name) {

        StoreEntity store = getStoreByName(name);

        if (store != null) {

            em.remove(store);
        }

        return store;
    }

    public StoreEntity removeStoreById(int restoId){
        StoreEntity store = getStoreById(restoId);

        if (store != null) {

            em.remove(store);
        }

        return store;
    }

    /*
    public List<DishEntity> getAllDishes() {

        Query getStore;

        try {

            getStore = em.createNamedQuery("findAllProducts");


        } catch (Exception e) {

            getStore = null;
        }

        return (List<DishEntity>) getStore.getResultList();
    }
     * */
}
